var cellList = {
	cellobject : null,
	startrow : 0,
	vars : [],
	isRender : false,
	insertCell : function(elm, base, w, h) {
		if (!document.getElementById(elm))
			return -1;
		var str = '';
		str += '<object name=webCellobj id=webCellobj width="'+ w+ '" height="'+ h+ '" classid="CLSID:3F166327-8030-4881-8BD2-EA25350E574A" codebase="'+ base + '">';
		str += '</object>';
		document.getElementById(elm).innerHTML = str;

		this.cellobject = document.getElementById('webCellobj');

	},
	exportFile : function() {
		this.cellobject.exportFile();
	},
	print : function() {
		this.cellobject.PrintSetOrient(1); // 2006-10-30 add by
											// dfz,设置打印时进纸方向为横向
		this.cellobject.PrintPreview(1, 0);
	},

	insertRow : function(setData, startRow) {
		var cell = this.cellobject;

		var varsLength = this.vars.length;
		for (var j = 0; j < setData.length; j++) {
			var data = setData[j];
			var lastRow = j + startRow;
			for (var i = 0; i < varsLength; i++) {
				var cellvar = this.vars[i];
				var name = cellvar.name;
				if (data[name] != null) {
					if (typeof data[name] === 'number') {
						cell.D(cellvar.col, lastRow, 0, data[name]);
					} else
						cell.S(cellvar.col, lastRow, 0, data[name]);
				}

			}
		}
		cell.CalculateAll();
		cell.ProtectFormula = true;

	},
	findVars : function() {

		var countRows = this.cellobject.getRows(0);
		var countCols = this.cellobject.getCols(0);
		for (var i = 1; i < countRows; i++) {
			for (var j = 1; j < countCols; j++) {
				var varname = this.cellobject.findCellVar(j, i, 0);
				if (varname && varname != '') {
					var varinfo = {
						name : varname,
						col : j,
						row : i
					};
					if (this.startrow < i)
						this.startrow = i
					this.vars.push(varinfo);

				}
			}
		}

		return this.vars;
	},
	// 'budget/images/CellWeb5.CAB'
	render : function(DivID, base, w, h, cellurl) {
		if (this.insertCell(DivID, base, w, h) === -1)
			return -1;
		if (this.cellobject.Login("Beelink software", "", "13040483","7140-0421-0057-6004") == 0) {
			alert("注册失败");
			return -1;
		}
		rtn = this.cellobject.OpenFile(cellurl, "");
		if (rtn != 1) {
			alert("打开Cell模板文件失败(" + rtn + "URL:" + cellurl + ")!");
			return -1;
		}
		this.isRender = true;
	},
	deleteAllRow : function() {
		var cell = this.cellobject;
		var cont = cell.getRows(0) - this.startrow - 1;
		if (cont > 0) {
			cell.DeleteRow(this.startrow + 1, cont, 0);
		}
	},
	setCellFormat : function(setDatalength, startrow) {
		var cell = this.cellobject;
		if (setDatalength !== 0) {
			var isstart = this.startrow == cell.getRows(0) - 1;
			cell.InsertRow(startrow + 1, isstart? setDatalength - 1: setDatalength, 0);
			var cols = cell.getCols(0);
			var rows = cell.getRows(0);
			for (var j = 1; j < cols; j++) {
				if (cell.IsFormulaCell(j, startrow, 0)) {
					cell.SelectRange(j, startrow, j, rows);
					cell.FormulaFillSerial();
				}
			}
			cell.ClearSelection();
		}
	},

	init : function(cellurl, DivID, base, w, h) {
		
		if (!this.isRender){
			if(!DivID){
					var celldiv = document.createElement('div');
					celldiv.id = 'celldiv';
					document.body.appendChild(celldiv);
					DivID='celldiv';
			}
			if(!base)base='images/CellWeb5.CAB';
			if(!w)w='100%';
			if(!h)h='100%';
			if(!cellurl)cellurl="tmp.cll";
			this.render(DivID, base, w, h, cellurl);
		}
		if (this.vars.length === 0)
			this.findVars();
		this.rowHeigth = this.cellobject.GetRowHeight(0, this.startrow, 0);
		this.cellobject.SetDefaultRowHeight(0, 0, this.rowHeigth);
		this.deleteAllRow();

	},
	setData : function(setData, cellurl) {
		if (this.vars.length === 0)
			this.findVars();
		if (setData.length > 1) {
			this.deleteAllRow();
			this.setCellFormat(setData.length, this.startrow);
		}
		this.insertRow(setData, this.startrow);

	},
	addData : function(setData, cellurl) {

		if (this.vars.length === 0)
			this.findVars();
		var startrow = this.cellobject.getRows(0) - 1;
		if (setData.length > 1) {
			this.setCellFormat(setData.length, startrow);

		}

		this.insertRow(setData, startrow + 1);

	}
}